package com.mrogrape.notepad.ui.find;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;

import org.fife.ui.rtextarea.SearchContext;

import com.jidesoft.swing.JideButton;
import com.mrogrape.notepad.activity.MainFrame;
import com.mrogrape.notepad.common.ViewUtils;
import com.mrogrape.notepad.data.ViewContext;
import com.mrogrape.notepad.resource.Settings;
import com.mrogrape.notepad.ui.HistoryTextField;
import com.mrogrape.notepad.ui.editor.TextEditor;
import com.mrogrape.util.StringUtils;

public class FindDialog extends JDialog{

	/** serialVersionUID */
	private static final long serialVersionUID=-1L;

	public FindDialog(MainFrame frame){
		super((Frame) frame);
		
		this.frame=frame;
		this.findDialgoSettings = Settings.getSettings().getFindDialogSettings();
		
		this.initializeComponents();
		this.applyPreferences();
	}

	public void setFindContent(String text){

		if(StringUtils.isNotEmpty(text)){

			this.findText.setText(text);
		}
	}
	
	public String getFindContent(){
		
		return this.findText.getText();
	}
	
	@Override
	public void setVisible(boolean aFlg){
		super.setVisible(aFlg);
		this.findText.requestFocus();
		this.findText.selectAll();
	}

	protected void thisWindowGainedFocus(WindowEvent arg0){

		this.currentEditor=this.frame.getCurrentTextEditor();

		this.setAllActionEnable(false);
		if(this.currentEditor != null){

			this.findBtn.setEnabled(true);
			this.replaceAllBtn.setEnabled(true);
		}
	}

	protected void findBtnActionPerformed(ActionEvent evt){

		if(this.currentEditor != null){
			
			String searchFor = this.findText.getText();
			
			if(!StringUtils.isEmptyAfterTrim(searchFor)){
				
				List<String> searchHistory = this.findDialgoSettings.getSearchForHistory();
				if(!searchHistory.contains(searchFor)){
					
					if(searchHistory.size() >= this.findDialgoSettings.getSearchForHistoryCapacity()){
						
						searchHistory.remove(searchHistory.size() - 1);
					}
					
					searchHistory.add(0, searchFor);
				}
				
				this.findText.setHistory(searchHistory);
			}

			boolean re=this.currentEditor.find(this.buildSearchContext(), this.warpSearchCheckbox.isSelected());

			if(!re){

				this.setInfomation("String Not Found");
				this.replaceBtn.setEnabled(false);
				this.replaceAndFindBtn.setEnabled(false);
			}else{

				this.replaceBtn.setEnabled(true);
				this.replaceAndFindBtn.setEnabled(true);
				this.setInfomation("");
			}
		}
	}

	protected void replaceBtnActionPerformed(ActionEvent arg0){

		if(this.currentEditor != null){

			boolean re=this.currentEditor.replace(this.buildSearchContext());

			if(!re){

				this.setInfomation("String Not Found");
				Toolkit.getDefaultToolkit().beep();
			}else{

				this.setInfomation("");
			}
		}

		this.replaceBtn.setEnabled(false);
		this.replaceAndFindBtn.setEnabled(false);
	}

	protected void replaceAndFindBtnActionPerformed(ActionEvent arg0){

		if(this.currentEditor != null){

			boolean re=this.currentEditor.replace(this.buildSearchContext());

			if(!re){

				this.replaceAndFindBtn.setEnabled(false);
				this.replaceBtn.setEnabled(false);
				this.setInfomation("String Not Found");
				Toolkit.getDefaultToolkit().beep();
			}else{

				this.setInfomation("");
				this.findBtnActionPerformed(null);
			}
		}

	}

	protected void replaceAllBtnActionPerformed(ActionEvent arg0){

		if(this.currentEditor != null){

			this.currentEditor.replaceAll(this.buildSearchContext());
			this.replaceBtn.setEnabled(false);
			this.replaceAndFindBtn.setEnabled(false);
			
			
			String replaceWith = this.replaceText.getText();
			
			if(!StringUtils.isEmptyAfterTrim(replaceWith)){
				
				List<String> replaceHistory = this.findDialgoSettings.getReplaceWithHistory();
				if(!replaceHistory.contains(replaceWith)){
					
					if(replaceHistory.size() >= this.findDialgoSettings.getReplaceWithHistoryCapacity()){
						
						replaceHistory.remove(replaceHistory.size() - 1);
					}
					
					replaceHistory.add(0, replaceWith);
				}
				
				this.replaceText.setHistory(replaceHistory);
			}
		}
	}

	protected void closeBtnActionPerformed(ActionEvent arg0){

		this.setVisible(false);
	}
	
	protected void findTextActionPerformed(ActionEvent e) {
		
		findBtnActionPerformed(null);
	}
	protected void forwardRadioActionPerformed(ActionEvent e) {
		
		this.findDialgoSettings.setSearchBackward(this.backwardRadio.isSelected());
	}
	protected void backwardRadioActionPerformed(ActionEvent e) {
		
		this.findDialgoSettings.setSearchBackward(this.backwardRadio.isSelected());
	}
	protected void scopeAllRadioActionPerformed(ActionEvent e) {
		
		this.findDialgoSettings.setSelectionOnly(this.scopeSelectionRadio.isSelected());
	}
	protected void scopeSelectionRadioActionPerformed(ActionEvent e) {
		
		this.findDialgoSettings.setSelectionOnly(this.scopeSelectionRadio.isSelected());
	}
	
	protected void matchCaseCheckboxActionPerformed(ActionEvent e) {
		
		this.findDialgoSettings.setMatchCase(this.matchCaseCheckbox.isSelected());
	}
	
	protected void warpSearchCheckboxActionPerformed(ActionEvent e) {
		
		this.findDialgoSettings.setWrapSearch(this.warpSearchCheckbox.isSelected());
	}
	
	protected void wholeWordCheckboxActionPerformed(ActionEvent e) {
	
		this.findDialgoSettings.setWholeWord(this.wholeWordCheckbox.isSelected());
	}
	
	protected void regexCheckboxActionPerformed(ActionEvent e) {
		
		this.findDialgoSettings.setRegularExpressions(this.regexCheckbox.isSelected());
	}

	private void initializeComponents(){

		this.setMinimumSize(new Dimension(370,445));
		this.setName("findDialog");
		this.setTitle("Find/Replace");
		this.addWindowFocusListener(new WindowFocusListener(){
			@Override
			public void windowGainedFocus(WindowEvent arg0){
				FindDialog.this.thisWindowGainedFocus(arg0);
			}

			@Override
			public void windowLostFocus(WindowEvent arg0){}
		});

		JPanel contentPanel=new JPanel();
		this.getContentPane().add(contentPanel,BorderLayout.CENTER);

		JPanel findContentPanel=new JPanel();

		JPanel directionPanel=new JPanel();
		directionPanel.setBorder(new TitledBorder(null,"Direction",TitledBorder.LEADING,TitledBorder.TOP,null,null));

		JPanel scopePanel=new JPanel();
		scopePanel.setBorder(new TitledBorder(null,"Scope",TitledBorder.LEADING,TitledBorder.TOP,null,null));

		this.scopeAllRadio=new JRadioButton("All");
		this.scopeAllRadio.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				scopeAllRadioActionPerformed(e);
			}
		});
		this.scopeButtonGroup.add(this.scopeAllRadio);
		this.scopeAllRadio.setSelected(true);

		this.scopeSelectionRadio=new JRadioButton("Selection");
		this.scopeSelectionRadio.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				scopeSelectionRadioActionPerformed(e);
			}
		});
		this.scopeButtonGroup.add(this.scopeSelectionRadio);
		this.scopeSelectionRadio.setEnabled(false);
		GroupLayout gl_scopePanel=new GroupLayout(scopePanel);
		gl_scopePanel.setHorizontalGroup(gl_scopePanel.createParallelGroup(Alignment.LEADING).addGap(0,168,Short.MAX_VALUE).addGroup(gl_scopePanel.createSequentialGroup().addContainerGap().addGroup(gl_scopePanel.createParallelGroup(Alignment.LEADING).addComponent(this.scopeAllRadio).addComponent(this.scopeSelectionRadio)).addContainerGap(29,Short.MAX_VALUE)));
		gl_scopePanel.setVerticalGroup(gl_scopePanel.createParallelGroup(Alignment.LEADING).addGap(0,83,Short.MAX_VALUE).addGroup(gl_scopePanel.createSequentialGroup().addContainerGap().addComponent(this.scopeAllRadio).addPreferredGap(ComponentPlacement.RELATED).addComponent(this.scopeSelectionRadio).addContainerGap(28,Short.MAX_VALUE)));
		scopePanel.setLayout(gl_scopePanel);

		JPanel optionPanel=new JPanel();
		optionPanel.setBorder(new TitledBorder(null,"Options",TitledBorder.LEADING,TitledBorder.TOP,null,null));

		JPanel buttonPanel=new JPanel();

		JPanel infoPanel=new JPanel();
		GroupLayout gl_contentPanel=new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING).addComponent(findContentPanel,GroupLayout.DEFAULT_SIZE,404,Short.MAX_VALUE).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(directionPanel,GroupLayout.DEFAULT_SIZE,181,Short.MAX_VALUE).addPreferredGap(ComponentPlacement.RELATED).addComponent(scopePanel,GroupLayout.DEFAULT_SIZE,197,Short.MAX_VALUE).addContainerGap()).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(optionPanel,GroupLayout.DEFAULT_SIZE,384,Short.MAX_VALUE).addContainerGap()).addGroup(Alignment.LEADING,gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(buttonPanel,GroupLayout.DEFAULT_SIZE,384,Short.MAX_VALUE).addContainerGap()).addGroup(Alignment.LEADING,gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(infoPanel,GroupLayout.DEFAULT_SIZE,384,Short.MAX_VALUE).addContainerGap()));
		gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addComponent(findContentPanel,GroupLayout.PREFERRED_SIZE,74,GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(directionPanel,GroupLayout.PREFERRED_SIZE,83,GroupLayout.PREFERRED_SIZE).addComponent(scopePanel,GroupLayout.PREFERRED_SIZE,83,GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED).addComponent(optionPanel,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED).addComponent(buttonPanel,GroupLayout.PREFERRED_SIZE,66,GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED).addComponent(infoPanel,GroupLayout.PREFERRED_SIZE,39,GroupLayout.PREFERRED_SIZE).addContainerGap(32,Short.MAX_VALUE)));

		this.infoView=new JLabel();

		JButton closeBtn=new JButton("Close");
		closeBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				FindDialog.this.closeBtnActionPerformed(arg0);
			}
		});
		GroupLayout gl_infoPanel=new GroupLayout(infoPanel);
		gl_infoPanel.setHorizontalGroup(gl_infoPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_infoPanel.createSequentialGroup().addContainerGap().addComponent(this.infoView).addPreferredGap(ComponentPlacement.RELATED,217,Short.MAX_VALUE).addComponent(closeBtn).addContainerGap()));
		gl_infoPanel.setVerticalGroup(gl_infoPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_infoPanel.createSequentialGroup().addContainerGap().addGroup(gl_infoPanel.createParallelGroup(Alignment.BASELINE).addComponent(this.infoView).addComponent(closeBtn)).addContainerGap(18,Short.MAX_VALUE)));
		infoPanel.setLayout(gl_infoPanel);

		this.replaceAllBtn=new JButton("Replace All");
		this.replaceAllBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				FindDialog.this.replaceAllBtnActionPerformed(arg0);
			}
		});

		this.replaceAndFindBtn=new JButton("Replace/Find");
		this.replaceAndFindBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				FindDialog.this.replaceAndFindBtnActionPerformed(arg0);
			}
		});

		this.replaceBtn=new JButton("Replace");
		this.replaceBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				FindDialog.this.replaceBtnActionPerformed(arg0);
			}
		});

		this.findBtn=new JButton("Find");
		this.findBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				FindDialog.this.findBtnActionPerformed(arg0);
			}
		});
		GroupLayout gl_buttonPanel=new GroupLayout(buttonPanel);
		gl_buttonPanel.setHorizontalGroup(gl_buttonPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_buttonPanel.createSequentialGroup().addContainerGap(119,Short.MAX_VALUE).addGroup(gl_buttonPanel.createParallelGroup(Alignment.TRAILING,false).addComponent(this.findBtn,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addComponent(this.replaceAndFindBtn,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_buttonPanel.createParallelGroup(Alignment.TRAILING,false).addComponent(this.replaceBtn,Alignment.LEADING,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addComponent(this.replaceAllBtn,Alignment.LEADING,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)).addContainerGap()));
		gl_buttonPanel.setVerticalGroup(gl_buttonPanel.createParallelGroup(Alignment.TRAILING).addGroup(gl_buttonPanel.createSequentialGroup().addContainerGap(GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addGroup(gl_buttonPanel.createParallelGroup(Alignment.BASELINE).addComponent(this.replaceBtn).addComponent(this.findBtn)).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_buttonPanel.createParallelGroup(Alignment.BASELINE).addComponent(this.replaceAllBtn).addComponent(this.replaceAndFindBtn)).addContainerGap()));
		buttonPanel.setLayout(gl_buttonPanel);

		this.matchCaseCheckbox=new JCheckBox("Match Case");
		this.matchCaseCheckbox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				matchCaseCheckboxActionPerformed(e);
			}
		});

		this.wholeWordCheckbox=new JCheckBox("Whole Word");
		this.wholeWordCheckbox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				wholeWordCheckboxActionPerformed(e);
			}
		});

		this.warpSearchCheckbox=new JCheckBox("Wrap Search");
		this.warpSearchCheckbox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				warpSearchCheckboxActionPerformed(e);
			}
		});

		this.regexCheckbox=new JCheckBox("Regular Expressions");
		this.regexCheckbox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				regexCheckboxActionPerformed(e);
			}
		});
		GroupLayout gl_optionPanel=new GroupLayout(optionPanel);
		gl_optionPanel.setHorizontalGroup(gl_optionPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_optionPanel.createSequentialGroup().addContainerGap().addGroup(gl_optionPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_optionPanel.createSequentialGroup().addComponent(this.matchCaseCheckbox).addGap(18).addComponent(this.warpSearchCheckbox)).addComponent(this.wholeWordCheckbox).addComponent(this.regexCheckbox)).addContainerGap(93,Short.MAX_VALUE)));
		gl_optionPanel.setVerticalGroup(gl_optionPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_optionPanel.createSequentialGroup().addContainerGap().addGroup(gl_optionPanel.createParallelGroup(Alignment.BASELINE).addComponent(this.matchCaseCheckbox).addComponent(this.warpSearchCheckbox)).addPreferredGap(ComponentPlacement.RELATED).addComponent(this.wholeWordCheckbox).addPreferredGap(ComponentPlacement.RELATED).addComponent(this.regexCheckbox).addContainerGap(15,Short.MAX_VALUE)));
		optionPanel.setLayout(gl_optionPanel);

		this.forwardRadio=new JRadioButton("Forward");
		this.forwardRadio.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				forwardRadioActionPerformed(e);
			}
		});
		this.forwardRadio.setSelected(true);
		this.directionButtonGroup.add(this.forwardRadio);

		this.backwardRadio=new JRadioButton("Backward");
		this.backwardRadio.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				backwardRadioActionPerformed(e);
			}
		});
		this.directionButtonGroup.add(this.backwardRadio);
		GroupLayout gl_directionPanel=new GroupLayout(directionPanel);
		gl_directionPanel.setHorizontalGroup(gl_directionPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_directionPanel.createSequentialGroup().addContainerGap().addGroup(gl_directionPanel.createParallelGroup(Alignment.LEADING).addComponent(this.forwardRadio).addComponent(this.backwardRadio)).addContainerGap(29,Short.MAX_VALUE)));
		gl_directionPanel.setVerticalGroup(gl_directionPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_directionPanel.createSequentialGroup().addContainerGap().addComponent(this.forwardRadio).addPreferredGap(ComponentPlacement.RELATED).addComponent(this.backwardRadio).addContainerGap(28,Short.MAX_VALUE)));
		directionPanel.setLayout(gl_directionPanel);

		JLabel findLabel=new JLabel("Find:");

		this.findText=new HistoryTextField();
		this.findText.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				findTextActionPerformed(e);
			}
		});
		this.findText.setFont(ViewContext.getDefaultMonospacedFont());
		this.findText.setMinimumSize(new Dimension(50,19));

		JideButton findCharSelector=new JideButton();
		findCharSelector.setFont(ViewContext.getDefaultMonospacedFont());
		findCharSelector.setText("\u25BC");
		findCharSelector.addActionListener(new AssistantButtonMenu(this.findDialgoSettings.getSearchForHistory(), this.findText));

		JLabel replareLabel=new JLabel("Replace With:");

		this.replaceText=new HistoryTextField();
		this.replaceText.setFont(ViewContext.getDefaultMonospacedFont());
		this.replaceText.setMinimumSize(new Dimension(50,19));

		JideButton replaceCharSelector=new JideButton();
		replaceCharSelector.setFont(ViewContext.getDefaultMonospacedFont());
		replaceCharSelector.setText("\u25BC");
		replaceCharSelector.addActionListener(new AssistantButtonMenu(this.findDialgoSettings.getReplaceWithHistory(), this.replaceText));
		
		GroupLayout gl_findContentPanel=new GroupLayout(findContentPanel);
		gl_findContentPanel.setHorizontalGroup(
			gl_findContentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_findContentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_findContentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(replareLabel)
						.addComponent(findLabel))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_findContentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(this.findText, GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
						.addComponent(this.replaceText, GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_findContentPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(findCharSelector, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(replaceCharSelector, GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE))
					.addGap(16))
		);
		gl_findContentPanel.setVerticalGroup(
			gl_findContentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_findContentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_findContentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_findContentPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(findLabel)
							.addComponent(this.findText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(findCharSelector, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_findContentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_findContentPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(replareLabel)
							.addComponent(this.replaceText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(replaceCharSelector, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		findContentPanel.setLayout(gl_findContentPanel);
		contentPanel.setLayout(gl_contentPanel);

		ViewUtils.setCenterParent((Component) this.frame,this);
	}
	
	private void applyPreferences(){

		this.findText.setHistory(this.findDialgoSettings.getSearchForHistory());
		this.replaceText.setHistory(this.findDialgoSettings.getReplaceWithHistory());
		
		this.forwardRadio.setSelected(!this.findDialgoSettings.isSearchBackward());
		this.backwardRadio.setSelected(this.findDialgoSettings.isSearchBackward());
		this.scopeAllRadio.setSelected(!this.findDialgoSettings.isSelectionOnly());
		this.matchCaseCheckbox.setSelected(this.findDialgoSettings.isMatchCase());
		this.wholeWordCheckbox.setSelected(this.findDialgoSettings.isWholeWord());
		this.regexCheckbox.setSelected(this.findDialgoSettings.isRegularExpressions());
		this.warpSearchCheckbox.setSelected(this.findDialgoSettings.isWrapSearch());
	}

	private void setAllActionEnable(boolean enable){

		this.setSearchActionEnable(enable);
		this.setReplaceActionEnable(enable);

	}

	private void setSearchActionEnable(boolean enable){

		this.findBtn.setEnabled(enable);
	}

	private void setReplaceActionEnable(boolean enable){

		this.replaceBtn.setEnabled(enable);
		this.replaceAndFindBtn.setEnabled(enable);
		this.replaceAllBtn.setEnabled(enable);
	}

	private SearchContext buildSearchContext(){

		this.searchContext.setSearchFor(this.findText.getText());
		this.searchContext.setReplaceWith(this.replaceText.getText());

		this.searchContext.setSearchForward(this.forwardRadio.isSelected());
		this.searchContext.setMatchCase(this.matchCaseCheckbox.isSelected());
		this.searchContext.setRegularExpression(this.regexCheckbox.isSelected());
		this.searchContext.setWholeWord(this.wholeWordCheckbox.isSelected());
		this.searchContext.setSearchSelectionOnly(this.scopeSelectionRadio.isSelected());

		return this.searchContext;
	}

	private void setInfomation(String info){

		this.infoView.setText(info);
	}

	private TextEditor currentEditor;
	private SearchContext searchContext=new SearchContext();
	private FindDialogSettings findDialgoSettings = null;

	private MainFrame frame=null;
	private JRadioButton forwardRadio;
	private JRadioButton backwardRadio;
	private JRadioButton scopeAllRadio;
	private JRadioButton scopeSelectionRadio;
	private JCheckBox matchCaseCheckbox;
	private JCheckBox wholeWordCheckbox;
	private JCheckBox warpSearchCheckbox;
	private JCheckBox regexCheckbox;
	private JLabel infoView;
	private final ButtonGroup directionButtonGroup=new ButtonGroup();
	private JButton findBtn;
	private JButton replaceBtn;
	private JButton replaceAndFindBtn;
	private JButton replaceAllBtn;
	private final ButtonGroup scopeButtonGroup=new ButtonGroup();
	private HistoryTextField findText;
	private HistoryTextField replaceText;
}
